
(function(global){

    'use strict';

    const DOM_INDEX_KEY = "wf_dom_index";
    
    let viewDomIndex = 0;//dom随机ID
    const domEventMap = {};

    const ViewUtil = {};

    /**
     * dom 选择器
     */
    ViewUtil.query = function (selector, queryAll, parent) {

        let p = parent ? parent : document;
        if (queryAll) {
            return p.querySelectorAll(selector);
        } else {
            return p.querySelector(selector);
        }
    };
    
    /**
     * 为dom标记流程标识
     */
    ViewUtil.randId = function (el) {

        let elIndex = "";
        if(el === document){
            elIndex = document[DOM_INDEX_KEY];
        }else{
            elIndex = SVGUtil.getAttr(el, DOM_INDEX_KEY);
        }
        
        if (!elIndex) {
            
            elIndex = "wf_" + (viewDomIndex++);
            
            if(el === document){
                document[DOM_INDEX_KEY] = elIndex;
            }else {
                SVGUtil.setAttr(el, DOM_INDEX_KEY, elIndex);
            }
        }
        return elIndex;
    };
    
    
    /**
     * 保留 N 位有效数字
     * @param num
     * @param n
     * @param type true 去尾法, false - 收尾法
     * @private
     */
    ViewUtil.effectiveValue =  function(num, n, type){

        type = type !== false;
        let tempNum = Math.pow(10, n);
        let ret = num * tempNum;
        if(type){
            ret = Math.floor(ret);
        }else{
            ret = Math.ceil(ret);
        }
        return ret / tempNum;
    };
    
    /**
     * 绑定事件
     */
    ViewUtil.addEvent = function (el, type, fn, capture) {

        let _eventCompat = function (event) {
            let type = event.type;
            if (type === 'DOMMouseScroll' || type === 'mousewheel') {
                event.delta = (event.wheelDelta) ? event.wheelDelta / 120 : -(event.detail || 0) / 3;
            }
            //alert(event.delta);
            if (event.srcElement && !event.target) {
                event.target = event.srcElement;
            }
            if (!event.preventDefault && event.returnValue !== undefined) {
                event.preventDefault = function () {
                    event.returnValue = false;
                };
            }
            return event;
        };

        let tempFn = null;
        if (window.addEventListener) {
            if (type === "mousewheel" && document.mozFullScreen !== undefined) {
                type = "DOMMouseScroll";
            }
            el.addEventListener(type, tempFn = function (event) {
                fn.call(this, _eventCompat(event));
            }, capture || false);
        } else if (window.attachEvent) {
            el.attachEvent("on" + type, tempFn = function (event) {
                event = event || window.event;
                fn.call(el, _eventCompat(event));
            });
        }

        //缓存事件
        let elIndex = ViewUtil.randId(el);

        let eventFns = domEventMap[elIndex] || {};

        let es = eventFns[type] || {};

        let agents = es["agents"] || [], srcEvents = es["srcEvents"] || [];

        agents.push(tempFn);
        srcEvents.push(fn);

        es["agents"] = agents;
        es["srcEvents"] = srcEvents;

        eventFns[type] = es;
        domEventMap[elIndex] = eventFns;
    };
    
    
    /**
     * 移除事件
     */
    ViewUtil.removeEvent = function (el, type, fn) {

        let domIndex = ViewUtil.randId(el);

        let eventFns = domEventMap[domIndex] || {};
        let es = eventFns[type] || {};
        let agents = es["agents"] || [], srcEvents = es["srcEvents"] || [];

        let tempFn;
        for (let i = 0, len = srcEvents.length; i < len; i++) {
            if (fn === srcEvents[i]) {
                tempFn = agents[i];

                agents.splice(i, 1);
                srcEvents.splice(i, 1);
                break;
            }
        }

        if (!tempFn) {
            //没有找到对应的事件
            return;
        }

        es["agents"] = agents;
        es["srcEvents"] = srcEvents;

        eventFns[type] = es;
        domEventMap[domIndex] = eventFns;


        if (window.addEventListener) {
            if (type === "mousewheel" && document.mozFullScreen !== undefined) {
                type = "DOMMouseScroll";
            }
            el.removeEventListener(type, tempFn);
        } else if (window.attachEvent) {
            el.detachEvent("on" + type, tempFn);
        }
    };
    
    // 注入到window中
    global.ViewUtil = ViewUtil;
})(window);
